/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function calTime(){
    var currentETime = new Date();
    var eTime = currentETime.getTime();
    $('#responseTime').html(eTime - sTime);
}
var sTime;
function startTime(){
    var currentSTime = new Date();
    sTime = currentSTime.getTime();
    $('#responseTime').html("<img src=\"./images/loader.gif\" />");
}

 
function editBo(hgabo1,shelf1){
    $.blockUI();
    $.post(
        "RoomSettingServlet",
        {
            action: "loadBoData",
            hgabo:hgabo1,
            shelf:shelf1
        },
        function(data){
            if(data.success){
                $("#shelf").val(shelf1);
                $("#hgabo").val(hgabo1);
                $("#tsr").val(data.tsr);
                $("#tab").val(data.tab);
                $("#qty").val(data.qty);
                $("#partnum").val(data.partnum);
                $("#qty").focus();
                
            }
          
        },
        "json"

        );
   
}
function deleteBo(hgabo1,shelf1){
   
    if( confirm("Do you want to edit HGA BO :"+hgabo1+" in shelf "+shelf1+"? ")){
    $.blockUI();
    $.post(
        "RoomSettingServlet",
        {
            action: "deleteBoData",
            hgabo:hgabo1,
            shelf:shelf1
        },
        function(data){
            if(data.success){
                loadshelfinfotable(shelf1);
                loadRoom();
            }else{
                alert("Can't delete data");
            }

        },
        "json"

        );
    }
}
function emptyShelf(shelf1){
    
    if( confirm("Do you want to empty shelf "+shelf1+" data ? ")){
    $.blockUI();
    $.post(
        "RoomSettingServlet",
        {
            action: "emptyData",

            shelf:shelf1
        },
        function(data){
            if(data.success){
                loadshelfinfotable(shelf1);
                loadRoom();
            }else{
                alert("Can't empty data");
            }

        },
        "json"

        );
    }
}
 function loadRoomInfoTable(){
      $.blockUI();
 $.post("LoadRoomTable",{
        action: "loadroominfotable"
    },
    function(data){

        $("#showTable").html(data);

    },
    "html"

    );
}
function loadshelfinfotable(shelf1){
    $.post(
                "LoadRoomTable",
                {
                    action: "loadshelftable",
                    edit: "Y",
                    shelf: shelf1
                },
                function(data){
                    $("#shelfinfotable").html(data)
                },
                "html"

                );
}
function loadRoom(){
    $.blockUI();
    $.post("LoadRoomTable",{
        action: "loadroom"
    },
    function(data){

        for( i in data )
        {

            $('#'+i).attr("src", data[i]);


        }

    },
    "json"

    );
        $.blockUI();
   $.post("LoadRoomTable",{
        action: "loadroomqty"
    },
    function(data){

        for( i in data )
        {

            $('#QTY'+i).text(data[i]);


        }

    },
    "json"

    );
     //เรียกใช้ BlockUI
}

function  loadRoomInfo(){
    $.blockUI();
    $.post("LoadRoomTable",{
        action: "loadroominfo",
        shelf:$("#shelf").val()
    },
    function(data){
        if(data.success){
           
            $("#productname").text(data.productname+"-"+data.tgatype);
            //$("#tgatype").val(data.tgatype);
            //$("bondpad").val(data.bondpad);
            $('#room img').each(function()
            {
                $(this).attr('class', '');
            });
            $("#"+data.shelfno).attr('class', 'rollover_img');

        }else{
            alert("can't load shelf data");
            $("#shelf").val("");
            $("#productname").text("");
            $("#hgabo").val("");
            $("#qty").val("");
        }

    },
    "json"

    );
    
}
function checkshelfcap(shelf1,qty1){
    $.blockUI();
    $.post(
        "RoomSettingServlet",
        {
            action: "checkshelfcap",
            shelf: shelf1 ,
            qty:qty1
        },
        function(data){
            return  data.success;

        },
        "json"

        );
    
}
function checkhgaboduplicate(shelf1,hgabo1){
    $.blockUI();
    $.post(
        "RoomSettingServlet",
        {
            action: "checkhgaboduplicate",
            shelf: shelf1 ,
            hgabo:hgabo1
        },
        function(data){
            return  data.success;
        },
        "json"

        );
    
}
function findBoData(hgabo1){
    $.blockUI();
    $.post(
        "RoomSettingServlet",
        {
            action: "findBoData",
            hgabo:hgabo1
        },
        function(data){
            return  data;
        },
        "json"

        );
    
}
function updateHGABOQty(shelf1,hgabo1,qty1,action1){

    $.post(
        "RoomSettingServlet",
        {
            action: action1,
            shelf: shelf1 ,
            hgabo:hgabo1,
            qty:qty1
        },
        function(data){
            if(data.success){
                alert("Success to add data");
                loadRoom();
                $("#shelf").val("");
                $("#productname").text("");
                $("#hgabo").val("");
                $("#qty").val("");
            }else{
                alert("Error!! can't add data");
            }
        },
        "json"

        );

}
function addHGABOQty(shelf1,hgabo1,tsr1,partnum1,tab1,qty1){

    $.post(
        "RoomSettingServlet",
        {
            action: "addshelfbo",
            shelf: shelf1 ,
            hgabo:hgabo1,
            qty:qty1,
            tsr:tsr1,
            partnum:partnum1,
            tab:tab1
        },
        function(data){
            if(data.success){
                alert("Success to add data");
                loadRoom();
                $("#shelf").val("");
                $("#productname").text("");
                $("#hgabo").val("");
                $("#qty").val("");
            }else{
                alert("Error!! can't add data");
            }
        },
        "json"

        );
     
}

$(document).ready(function()
{
    loadRoom();
    // Make sure to only match links to wikipedia with a rel tag
    $('#room img').each(function()
    {
        // We make use of the .each() loop to gain access to each element via the "this" keyword...
        $(this).qtip(
        {
            content: {
                text: 'Loading...', // The text to use whilst the AJAX request is loading
                ajax: {
                    url: 'LoadRoomTable', // URL to the local file
                    type: 'POST', // POST or GET
                    data: "action=loadroomtooltip&shelf="+$(this).attr('id'), // Data to pass along with your request
                    success: function(data, status) {
                        // Process the data

                        // Set the content manually (required!)
                        this.set('content.text', data);
                    }
                }
            },
            position: {
                my: 'left center',  // Position my top left...
                at: 'right center', // at the bottom right of...
                adjust: {
                    screen: true
                } // Keep the tooltip on-screen at all times
            },
            style: {
                classes: "ui-tooltip-dark"

            }

        })


    });
    $('#room img').each(function()
    {
        $(this).click(function() {
            $('#shelfinfotable').html("<img src=\"./images/loader.gif\" />");
            $('#room img').each(function()
            {
                $(this).attr('class', '');
            });
            $(this).attr('class', 'rollover_img');
            $("#shelf").val($(this).attr('id'));
            $.post(
                "LoadRoomTable",
                {
                    action: "loadshelftable",
                    edit: "Y",
                    shelf: $(this).attr('id')
                },
                function(data){
                    $("#shelfinfotable").html(data)
                },
                "html"

                );
            loadRoomInfo();
          
        });
    });


    jQuery("#inputform").validationEngine('attach', {
        onValidationComplete: function(form, status){
            if(status){
                $.blockUI();
                var shelf1=$("#shelf").val();

                var hgabo1 = $("#hgabo").val();
                var qty1 = $("#qty").val();

                $.post(
                    "RoomSettingServlet",
                    {
                        action: "checkshelfcap",
                        shelf: shelf1 ,
                        qty:qty1
                    },
                    function(data){
                        if(data.success){
                            $.blockUI();
                            $.post(
                                "RoomSettingServlet",
                                {
                                    action: "checkhgaboduplicate",
                                    shelf: shelf1 ,
                                    hgabo:hgabo1
                                },
                                function(data){
                                     
                                    if(data.success){
                                        if(data.success2){

                                        if( confirm("This HGA BO have already in shelf =>"+data.shelfno+". Do you want to edit HGA BO quantity in shelf "+data.shelfno+"? ")){
                                             $.blockUI();
                                             $("#shelf").val(data.shelfno);
                                            $.post(
                                                "RoomSettingServlet",
                                                {
                                                        action: "updatehagboqty",
                                                        shelf: data.shelfno ,
                                                        hgabo:hgabo1,
                                                        qty:qty1,
                                                        tsr:$("#tsr").val(),
                                                        partnum:$("#partnum").val(),
                                                        tab:$("#tab").val()
                                                },
                                                function(data){
                                                    if(data.success){
                                                        alert("Success to add data");
                                                        loadRoom();
                                                        $("#shelf").val("");
                                                        $("#productname").text("");
                                                        $("#hgabo").val("");
                                                        $("#tab").val("");
                                                        $("#tsr").val("");
                                                        $("#partnum").val("");
                                                        $("#qty").val("");
                                                        loadshelfinfotable(shelf1);
                                                    }else{
                                                        alert("Error!! can't add data");
                                                    }
                                                },
                                                "json"

                                                );
                                        }
                                    } else{
                                        alert("This HGA BO have already in shelf =>"+data.shelfno+"");
                                    }
                                    }else{
                                        $.blockUI();
                                        $.post(
                                            "RoomSettingServlet",
                                            {
                                                action: "addshelfbo",
                                                shelf: shelf1 ,
                                                hgabo:hgabo1,
                                                qty:qty1,
                                                tsr:$("#tsr").val(),
                                                partnum:$("#partnum").val(),
                                                tab:$("#tab").val()
                                            },
                                            function(data){
                                                if(data.success){
                                                    alert("Success to add data");
                                                    loadRoom();
                                                    $("#shelf").val("");
                                                    $("#productname").text("");
                                                    $("#hgabo").val("");
                                                    $("#qty").val("");
                                                    $("#tsr").val("");
                                                    $("#tab").val("");
                                                    $("#partnum").val("");
                                                    loadshelfinfotable(shelf1);
                                                }else{
                                                    alert("Error!! can't add data");
                                                }
                                            },
                                            "json"

                                            );


                                    }
                                },
                                "json"

                                );

                        }else{
                            alert("If you add new bo ,Room \""+shelf1+"\" had full. Please clear room quantity ");
                        }
                        
                    },
                    "json"

                    );




                
            }
        }
    });

//    $('#hgabo').blur(function() {
//        var hgadata=$('#hgabo').val();        
//        if(hgadata!="" ){
//            $.blockUI();
//            $.post(
//                "RoomSettingServlet",
//                {
//                    action: "findBoData",
//                    hgabo:hgadata
//                },
//                function(data){                    
//                    if(data.success){                   
//                        $("#tsr").val(data.tsr);
//                        $("#tab").val(data.tab);
//                        $("#partnum").val(data.partnum);
//                    }else{
//                        alert("Can't find HGA BO NAME:\""+hgadata+"\" in ETS ");
//                    }
//                    
//                },
//                "json"
//
//                );
//            
//        }
//
//  
//    });
                 
});